

import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.*;

import org.w3c.dom.*;

import orm.Exchange;
import orm.ExchangeRate;
import orm.ExchangeRateDAO;
import exception.DbConnectionException;

public class KursyNBP {

	private Connection connection;
	
	private String driverClassName = "org.postgresql.Driver";
	private String connectionUrl = "jdbc:postgresql://localhost/kursynbp";
	private String dbUser = "kursynbp";
	private String dbPwd = "kursynbp";
	
	public KursyNBP() {
		try {
			Class.forName(driverClassName);
			connection = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (Exception ex) {
			throw new DbConnectionException();
		}
	}
	
	public Connection getDbConnection() {
		return this.connection;
	}
	
	private void updateRepository(Date date) {

		List<ExchangeRate> list = ExchangeRateDAO.find(date);
		if (list.isEmpty()) {
		
			URL urlReq = NBPService.request(date);
			if (urlReq != null) {
			
				XmlReader xmlReader = new XmlDomReader();
				Document xmlDoc = xmlReader.getXmlDocument(urlReq);
				XmlConverter<ExchangeRate> xmlToCrList = new XmlNBPExchangeRateConverter();
				list = xmlToCrList.convert(xmlDoc);

				ExchangeRateDAO.persist(list);
			}
		}
	}
	
	private void updateRepository(Date date1, Date date2) {
		
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		
		if (date1.before(date2)) {
			start.setTime(date1);
			end.setTime(date2);
		} else {
			start.setTime(date2);
			end.setTime(date1);
		}
		
		while (!start.after(end)) {
			Date date = start.getTime();
			
			updateRepository(date);
			
			start.add(Calendar.DATE, 1);
		}
	}

	public ExchangeRate getExchangeRate(Date date, String currCode) {
		
		updateRepository(date);
		
		return ExchangeRate.getExchangeRate(getDbConnection(), date, currCode);
    }
    
    public ExchangeRate getLowestExchangeRate(Date date) {

    	updateRepository(date);
    	
    	return ExchangeRate.getLowestExchangeRate(getDbConnection(), date);
    }
    
    public ExchangeRate getHighestExchangeRate(Date date) {
    	
		updateRepository(date);

		return ExchangeRate.getHighestExchangeRate(getDbConnection(), date);
    }
    
    public ExchangeRate getLowestExchangeRate(Date from, Date to) {
    	
		updateRepository(from, to);

		return ExchangeRate.getLowestExchangeRate(getDbConnection(), from, to);
    }

    public ExchangeRate getHighestExchangeRate(Date from, Date to) {
    	
		updateRepository(from, to);

		return ExchangeRate.getHighestExchangeRate(getDbConnection(), from, to);
    }

	public ExchangeRate getLowestExchangeRate(Exchange currency, Date from, Date to) {

		updateRepository(from, to);
		
		return ExchangeRate.getLowestExchangeRate(getDbConnection(), from, to, currency.getCode());
	}

	public ExchangeRate getHighestExchangeRate(Exchange currency, Date from, Date to) {

		updateRepository(from, to);
		
		return ExchangeRate.getHighestExchangeRate(getDbConnection(), from, to, currency.getCode());
	}

/*
    public ExchangeRateDiff getBiggestExchangeDiff(Date from, Date to) {
    	
    	return ExchangeRate.getBiggestExchangeDiff(getDbConnection(), from, to);
    }
*/
}